package com.by.controller;

import com.by.biz.IUserBiz;
import com.by.model.Result;
import com.by.model.User;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    //注入业务层
    @Autowired
    private IUserBiz iUserBiz;
    //从首页跳转到登录页
    @RequestMapping("/shouye")
    public ModelAndView toList(){
        return new ModelAndView("login");
    }
    //用于实现分页查询用户信息的功能
    @RequestMapping("/getPage")
    public Map queryUserByPage(int pageNum, int pageSize, User user){
        Map map = new HashMap();
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("user",user);
        Page page = iUserBiz.queryLikeUsers(map);
        map.put("page",page);
        map.put("totalPage",page.getPages());
        return map;
    }
    //用于实现添加用户的功能
    @RequestMapping("/add")
    public Result addUser(User user){
        iUserBiz.addUser(user);
        return new Result();
    }
    //用于实现删除用户信息
    @RequestMapping("/del")
    public Result delUser(User user){
        iUserBiz.delUser(user);
        return new Result();
    }
    //用于实现编辑用户信息
    @RequestMapping("/editpage")
    public ModelAndView findUser(User user){
        User rsUser = iUserBiz.getUser(user);
        Map<String,Object> rsMap = new HashMap<String,Object>();
        rsMap.put("data",rsUser);
        return new ModelAndView("edit",rsMap);
    }

    @RequestMapping("/edit")
    public Result editUser(User user){
        iUserBiz.editUser(user);
        return new Result();
    }

    @RequestMapping("/login")
    public ModelAndView login(User user){
        User rsUser = iUserBiz.loginUser(user);
        if(rsUser != null){
            Map<String,Object> rs = new HashMap<String,Object>();
            rs.put("data",rsUser);
            return new ModelAndView("list",rs);
        }else {
            return new ModelAndView("login");
        }
    }



    @RequestMapping("/regs")
    public ModelAndView regs(User user){
       int a = iUserBiz.addreg(user);
       if (a>0){
            return new ModelAndView("login");
       }else {
           return new ModelAndView("reg");
       }
    }
}

















